var _pqUtil = {};

_pqUtil.f = {
    getData: function ($grid) {
        return $grid.pqGrid("option").dataModel.data;
    },
    getTotalCount: function ($grid) {
        return $grid.pqGrid("option").dataModel.data.length;
    },
    /**
     * 清空
     * @param $grid
     */
    cleanGrid: function ($grid) {
        $grid.pqGrid("option", "dataModel.data", []);
        $grid.pqGrid("refreshDataAndView");
    },
    /**
     * 清空
     * @param $grid
     */
    cleanAndResetGrid: function ($grid) {
        console.log('pqgrid - cleanAndResetGrid');
        try {
            _pqUtil.f.cleanGrid($grid);
            _pqUtil.f.updateToolBar($grid, 0);
            $grid.pqGrid("reset", {sort: true, filter: true});
        } catch (e) {
            console.warn('pqgrid - cleanAndResetGrid.  error catch.');
        }
    },
    /**
     * 更新数量
     * @param $grid
     * @param total
     */
    updateToolBar: function ($grid, total) {
        console.log('pqgrid - updateToolBar');
        var realTotal, toolbar;
        if (total) {
            realTotal = total;
        } else {
            realTotal = _pqUtil.f.getTotalCount($grid);
        }

        toolbar = $grid.pqGrid("option", "toolbar");
        toolbar.items[1].type = "<span>共 " + realTotal + " 条</span>";
        $grid.pqGrid("refreshToolbar");
    },
    /**
     * 显示/隐藏列
     *
     * @param $grid
     * @param showColModels
     * @param hideColModels
     */
    showHideColumns: function ($grid, showColModels, hideColModels) {
        var i, leni,
            j, lenj,
            k, lenk,
            curDataIndx,
            allColumns = $grid.pqGrid("getColModel");

        for (i = 0, leni = allColumns.length; i < leni; i++) {
            curDataIndx = allColumns[i]['dataIndx'];
            for (j = 0, lenj = showColModels.length; j < lenj; j++) {
                if (curDataIndx === showColModels[j]['dataIndx']) {
                    showColModels[j].hidden = false;
                    break;
                }
            }
            if (j === lenj) {
                for (k = 0, lenk = hideColModels.length; k < lenk; k++) {
                    if (curDataIndx === hideColModels[k]['dataIndx']) {
                        hideColModels[k].hidden = true;
                        break;
                    }
                }
            }
        }
        console.log('pqgrid - showHideColumns');
    },
    createGrid: function ($grid, conf) {
    var defConf = {
      width: 'auto',
      height: '100%',
      showTitle: false,
      title: "",
      resizable: false,
      editable: false,
      hwrap: false,
      wrap: false,
      dragColumns: {enabled: false},
      numberCell: {
        show: true,
        width: 50
      },
      showBottom: false,
      showTop: true,
      showToolbar: true,
      virtualY: true,
      toolbar: {
        items: [{
          type: 'button',
          width: 150,
          label: "导出",
          icon: 'ion-ios-download-outline',
          listener: function () {
            var format = 'xls',
                blob = this.exportData({
                  format: format,
                  render: true
                });
            if (typeof blob === "string") {
              blob = new Blob([blob]);
            }
            saveAs(blob, "Defect List." + format);
          }
        }, {
          type: "<span>共 0 条</span>",
          width: 110
        }]
      },
      selectionModel: {
        type: 'row',
        mode: 'single'
      },
      scrollModel: {
        autoFit: false
      },
      collapsible: {
        on: false,
        collapsed: false,
        toggle: false,
      },
      colModel: [],
      dataModel: {
        data: [],
        location: "local"
      }
    };
    // $grid.pqGrid(true, $.extend({}, defConf, conf));
    $grid.pqGrid($.extend({}, defConf, conf));
  },
  createGrid2: function ($grid, conf) {
    var defConf = {
      width: 'auto',
      height: '230',
      freezeCols: 4,
      showTitle: false,
      title: "",
      resizable: false,
      hwrap: false,
      wrap: false,
      dragColumns: {enabled: false},
      numberCell: {
        show: true,
        width: 50
      },
      showBottom: false,
      showTop: true,
      showToolbar: true,
      virtualY: true,
      selectionModel: {
        type: 'row',
        mode: 'block'
      },
      toolbar: {
        items: [{
          type: 'button',
          width: 150,
          label: "导出",
          icon: 'ion-ios-download-outline',
          listener: function () {
            var format = 'xls',
                blob = this.exportData({
                  format: format,
                  render: true
                });
            if (typeof blob === "string") {
              blob = new Blob([blob]);
            }
            saveAs(blob, "Defect List." + format);
          }
        }, {
          type: "<span>共 0 条</span>",
          width: 110
        }]
      },
      scrollModel: {
        autoFit: false
      },
      collapsible: {
        on: false,
        collapsed: false,
        toggle: false,
      },
      colModel: [],
      dataModel: {
        data: [],
        location: "local"
      }
    };
    // $grid.pqGrid(true, $.extend({}, defConf, conf));
    $grid.pqGrid($.extend({}, defConf, conf));
  }
};